﻿/*

/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/												\
		Course	:	การสรุปผลของรายวิชาที่ลงทะเบียน
\												/


*/

var creditAll = 0,creditGeneral = 0,creditEnglish = 0,creditHuman = 0,creditSocial = 0,creditScience = 0,creditThai = 0,creditSE = 0,creditOther = 0;
$(function() {
	getcoursedata();
});

function getcoursedata() {
	window.creditAll = 0;window.creditGeneral = 0;window.creditEnglish = 0;window.creditHuman = 0;window.creditSocial = 0;
	window.creditScience = 0;window.creditThai = 0;window.creditSE = 0;window.creditOther = 0;
	var grade = 0,point = 0,getpregrade = 0;
	$.getJSON('class/_reqplan.php',function(data) {
		$.each(data,function(key,val) {
			if(val['grade'] != 'I' && val['grade'] != 'CX' && val['grade'] != '0')
			{
			
				if(val['sPreId'] != '')
				{
					$.get('class/_reqonepre.php',{'sid':val['sPreId'],'year':val['year'],'term':val['term']},function(data){
						getpregrade = gradeConverter(data);
						if(getpregrade != 0){
							point += gradeConverter(val['grade'])*parseInt(val['sCredit']);
							creditAll += parseInt(val['sCredit']);
							if(val['did'] == 1) {
								if(val['gid'] == 2) {
									creditGeneral += parseInt(val['sCredit']);
									creditEnglish += parseInt(val['sCredit']);
								}else if(val['gid'] == 3) {
									creditGeneral += parseInt(val['sCredit']);
									creditThai += parseInt(val['sCredit']);
								}else if(val['gid'] == 4) {
									creditGeneral += parseInt(val['sCredit']);
									creditHuman += parseInt(val['sCredit']);
								}else if(val['gid'] == 6) {
									creditGeneral += parseInt(val['sCredit']);
									creditSocial += parseInt(val['sCredit']);
								}else if(val['gid'] == 7) {
									creditGeneral += parseInt(val['sCredit']);
									creditScience += parseInt(val['sCredit']);
								}else {
									creditOther += parseInt(val['sCredit']);
								}
							}else if(val['did'] == 3) {
								if(val['gid'] == 20) {
									creditSE += parseInt(val['sCredit']);
								}else {
									creditOther += parseInt(val['sCredit']);
								}
							}else {
								creditOther += parseInt(val['sCredit']);
							}
							grade = point/creditAll;
							$('#reportUser').html('<table><tr><th>หน่วยกิตทั้งหมด : </th><th colspan ="2"> '+creditAll+' / 123 </th></tr><tr><td> หมวดวิชาศึกษาทั่วไป :</td><td colspan ="2"> '+creditGeneral+' / 31 </td></tr><tr><td> กลุ่มภาษาอังกฤษ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditEnglish+' / 12 </td></tr><tr><td> กลุ่มภาษาไทย : </td><td>&nbsp;▲&nbsp;</td><td> '+creditThai+' / 3 </td></tr><tr><td> กลุ่มวิชามนุษศาสตร์ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditHuman+' / 3 </td></tr><tr><td> กลุ่มวิชาสังคมศาสตร์ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditSocial+' / 6 </td></tr><tr><td> กลุ่มวิชาวิทยาศาสตร์และคณิตศาสตร์ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditScience+' / 7 </td></tr><tr><td> วิชาเฉพาะสาขาวิศวกรรมซอฟต์แวร์ : </td><td colspan ="2">'+creditSE+' / 86 </td></tr><tr><td> วิชาเลือกเสรี : </td><td colspan ="2">'+creditOther+' / 6</td></tr><tr><td> GPA :</td><td colspan="2">'+grade.toFixed(2)+'</td></tr></table');
						}
					});
				} else {
					$.get('class/_reqoneafter.php',{'id':val['id'],'sid':val['sid'],'year':val['year'],'term':val['term']},function(data){
						getpregrade = gradeConverter(data);
						if(getpregrade == 0){
							point += gradeConverter(val['grade'])*parseInt(val['sCredit']);
							creditAll += parseInt(val['sCredit']);
							if(val['did'] == 1) {
								if(val['gid'] == 2) {
									creditGeneral += parseInt(val['sCredit']);
									creditEnglish += parseInt(val['sCredit']);
								}else if(val['gid'] == 3) {
									creditGeneral += parseInt(val['sCredit']);
									creditThai += parseInt(val['sCredit']);
								}else if(val['gid'] == 4) {
									creditGeneral += parseInt(val['sCredit']);
									creditHuman += parseInt(val['sCredit']);
								}else if(val['gid'] == 6) {
									creditGeneral += parseInt(val['sCredit']);
									creditSocial += parseInt(val['sCredit']);
								}else if(val['gid'] == 7) {
									creditGeneral += parseInt(val['sCredit']);
									creditScience += parseInt(val['sCredit']);
								}else {
									creditOther += parseInt(val['sCredit']);
								}
							}else if(val['did'] == 3) {
								if(val['gid'] == 20) {
									creditSE += parseInt(val['sCredit']);
								}else {
									creditOther += parseInt(val['sCredit']);
								}
							}else {
								creditOther += parseInt(val['sCredit']);
							}
							grade = point/creditAll;
							$('#reportUser').html('<table><tr><th>หน่วยกิตทั้งหมด : </th><th colspan ="2"> '+creditAll+' / 123 </th></tr><tr><td> หมวดวิชาศึกษาทั่วไป :</td><td colspan ="2"> '+creditGeneral+' / 31 </td></tr><tr><td> กลุ่มภาษาอังกฤษ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditEnglish+' / 12 </td></tr><tr><td> กลุ่มภาษาไทย : </td><td>&nbsp;▲&nbsp;</td><td> '+creditThai+' / 3 </td></tr><tr><td> กลุ่มวิชามนุษศาสตร์ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditHuman+' / 3 </td></tr><tr><td> กลุ่มวิชาสังคมศาสตร์ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditSocial+' / 6 </td></tr><tr><td> กลุ่มวิชาวิทยาศาสตร์และคณิตศาสตร์ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditScience+' / 7 </td></tr><tr><td> วิชาเฉพาะสาขาวิศวกรรมซอฟต์แวร์ : </td><td colspan ="2">'+creditSE+' / 86 </td></tr><tr><td> วิชาเลือกเสรี : </td><td colspan ="2">'+creditOther+' / 6</td></tr><tr><td> GPA :</td><td colspan="2">'+grade.toFixed(2)+'</td></tr></table');
						}
					});
				}
			}
			grade = point/creditAll;
			$('#reportUser').html('<table><tr><th>หน่วยกิตทั้งหมด : </th><th colspan ="2"> '+creditAll+' / 123 </th></tr><tr><td> หมวดวิชาศึกษาทั่วไป :</td><td colspan ="2"> '+creditGeneral+' / 31 </td></tr><tr><td> กลุ่มภาษาอังกฤษ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditEnglish+' / 12 </td></tr><tr><td> กลุ่มภาษาไทย : </td><td>&nbsp;▲&nbsp;</td><td> '+creditThai+' / 3 </td></tr><tr><td> กลุ่มวิชามนุษศาสตร์ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditHuman+' / 3 </td></tr><tr><td> กลุ่มวิชาสังคมศาสตร์ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditSocial+' / 6 </td></tr><tr><td> กลุ่มวิชาวิทยาศาสตร์และคณิตศาสตร์ : </td><td>&nbsp;▲&nbsp;</td><td>'+creditScience+' / 7 </td></tr><tr><td> วิชาเฉพาะสาขาวิศวกรรมซอฟต์แวร์ : </td><td colspan ="2">'+creditSE+' / 86 </td></tr><tr><td> วิชาเลือกเสรี : </td><td colspan ="2">'+creditOther+' / 6</td></tr><tr><td> GPA :</td><td colspan="2">'+grade.toFixed(2)+'</td></tr></table');
				
		});	
	});
}

function gradeConverter(grade)
{
	var convet = 0;
	
	switch (grade)
	{
		case 'A': convet = 4;
				break;
		case 'B+': convet = 3.5;
				break;		
		case 'B': convet = 3;
				break;
		case 'C+': convet = 2.5;
				break;
		case 'C': convet = 2;
				break;
		case 'D+': convet = 1.5;
				break;
		case 'D': convet = 1;
				break;
		case 'F': convet = 0;
				break;
		case 'I': convet = 0;
				break;
		case 'CX': convet = 5;
				break;
		default:  convet = 0;
	}

	return convet;
}